package com.example.anydo.util.pub_util;


import android.content.Context;

import android.widget.Toast;

import androidx.annotation.RequiresApi;

import com.example.anydo.R;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class Util {

    private static Toast toast;
    public static void toast(Context context, String msg){
        if(toast != null)
            toast.cancel();
        toast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);
        toast.show();
    }

//获取今天
    public static String getDayNow() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date(System.currentTimeMillis());
        final String toDay = simpleDateFormat.format(date);
        return toDay;
    }

    //获取今天
    public static String getDayNowShow() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日");
        Date date = new Date(System.currentTimeMillis());
        final String toDay = simpleDateFormat.format(date);
        return toDay;
    }

    //获取下一天
    public static String nextDay(){
        //获取当前时间
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(date);
        //创建日历类用于获取下一天
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        //将天数加一
        calendar.add(Calendar.DATE, 1);
        date = calendar.getTime();
        //日期格式化
        String nextDateString = formatter.format(date);
        return nextDateString;
    }



    //1,2,3,4
    // 根据时间获取对应格式--> 0639 = 06:39
    public static String getTime(String strTime){
        return strTime.substring(0,2) + ":" + strTime.substring(2);
    }

    //1,2,3,4,   5,6  ,7,8
    // 根据日期获取对应格式--> 20210623 = 06.23
    public static String getDate(String strTime){
        return strTime.substring(4,6) + "." + strTime.substring(6);
    }

    // 根据日期获取星期几 //每周第一天是周日
    public static int getWeekday(String date){//必须yyyyMMdd
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        int day = Integer.parseInt(date.substring(6,8));
        Calendar c = new GregorianCalendar(year,month-1, day);//因为一月为0
        int m= c.get(Calendar.DAY_OF_WEEK);
        return m;
    }


    // 获取某年某月有多少天
    public static int getDaysOfMonth(String date){
        Calendar c = Calendar.getInstance();
        int year = Integer.parseInt(date.substring(0,4));
        int month = Integer.parseInt(date.substring(4,6));
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }
}
